<template>
  <div class="tab-title-page goods_list">
    <el-divider class="middle_divider" />
    <div class="child_page">
      <div class="common_form_search">
        <el-form ref="searchForm" :inline="true" :model="searchForm">
          <el-form-item label="商品ID">
            <el-input v-model="searchForm.goods_id" placeholder="请输入商品ID" clearable />
          </el-form-item>
          <el-form-item label="商品名称">
            <el-input v-model="searchForm.goods_name" placeholder="请输入商品名称" clearable />
          </el-form-item>
          <el-form-item label="商品品类">
            <el-select v-model="searchForm.cate_id" placeholder="请选择商品品类" clearable>
              <el-option v-for="item in categoryList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间" >
            <my-date-picker ref="myDatePicker" />
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" @click="search()">查询</el-button>
            <el-button size="small" @click="reset()">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-divider />
      <div style="padding:0 30px">
        <div class="flex align-center table-top-btns">
          <h5 class="flex align-center">
            <svg-icon style="margin-right:15px" icon-class="list" :size="15" />数据列表
          </h5>
          <el-button size="small" class="add-btn" icon="el-icon-plus" @click="handleAdd">发布商品</el-button>
        </div>
        <el-table v-show="tableData.length > -1" :data="tableData" border>
				<el-table-column align="center" prop="id" label="商品id"  width="80" />
				<el-table-column align="center" label="商品名称" prop="title" width="250" show-overflow-tooltip="true" />
				<el-table-column align="center" label="推广名称" prop="extension_title" show-overflow-tooltip="true" />
          <el-table-column align="center" label="商品图片" width="100">
            <template slot-scope="scope">
              <div class="flex align-center goods_info">
                <el-image style="width: 80px; height: 80px" :src="scope.row.img ? $globalObj.imgBaseUrl + scope.row.img : ''">
                  <div slot="error" class="image-slot">
                    <i class="el-icon-picture-outline" />
                  </div>
                </el-image>
              </div>
            </template>
          </el-table-column>
				<el-table-column align="center" label="商品类型" prop="meal_id"/>
          <el-table-column align="center" prop="goods_price" label="售价"  width="120"/>
          <el-table-column align="center" prop="goods_sort" label="排序"  width="120"/>
          <el-table-column align="center" label="库存" prop="num" width="80" />
          <el-table-column align="center" prop="brand_id" label="品类" width="80" />
          <el-table-column align="center" prop="create_time" label="创建时间" width="180" />
          <el-table-column align="center" prop="status" label="状态" width="80" />
          <el-table-column v-if="activeName == 2" align="center" prop="check_reason" label="原因" />
          <el-table-column fixed="right" align="center" label="操作">
            <template slot-scope="scope">
              <el-button type="warning" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button type="danger" size="mini" @click="handleDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <custom-page ref="customPage" :total="total" @getList="getList" />
      </div>
    </div>
    <off-dialog ref="offDialog" @getList="getList" />
    <set-dialog ref="setDialog" @getList="getList" />
    <share-dialog ref="shareDialog" />
    <lease-dialog ref="leaseDialog" :goods-list="goodsList" @getList="getList" />
    <goods-dialog ref="goodsDialog" dialog-width="1340px" :page-size="12" :append-to-body="true" :show-type-select="true" :show-checked-tool="true" @setSelect="setSelect" />
		<add-dialog ref="addDialog" :edit-item="editItem" @getList="getList"></add-dialog>
  </div>
</template>
<script>
import myDatePicker from '@/components/myDatePicker'
import offDialog from './components/offDialog'
import shareDialog from './components/shareDialog'
import setDialog from './components/setDialog'
import customPage from '@/components/customPage'
import leaseDialog from './components/leaseDialog'
import goodsDialog from './components/batchDialog'
import addDialog from './child/base.vue'
import { goodsGrounding, getGoodsAll, getCategory, updateGoods } from '@/api/audit'
import { getCategoryList, getGoodsList,deleteGoods } from '@/api/goods'
export default {
  components: { myDatePicker, offDialog, shareDialog, setDialog, customPage, leaseDialog, goodsDialog,addDialog },
  data() {
    return {
      searchForm: {
        goods_id: '',
        goods_name: '',
        shop_name: '',
        cate_id: '',
        rent_type: '',
        create_time: ''
      },
      activeName: '1',
      count: {
        status_put: 0,
        status_off: 0,
        status_lease: 0
      },
      total: 0,
      tableData: [],
      categoryList: [],
			dialogList:{},
      goodsList: {},
      editItem:{},
      editId:''
    }
  },
  mounted() {
    this.getSelectList()
    this.getList()
		
  },
  methods: {
		handleAdd(){
      this.editItem={}
			this.$refs.addDialog.dialogVisible=true
			this.$refs.addDialog.title='发布商品'
		},
    openSelect() {
      this.$refs.goodsDialog.dialogVisible = true
    },
    // 编辑商品
    handleEdit(item){
      this.editItem=item
			this.$refs.addDialog.dialogVisible=true
			this.$refs.addDialog.title='编辑商品'
    },
    // 删除商品
    handleDel(id) {
        this.$confirm('此操作将删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteGoods(id).then(res => {
          this.getList()
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
      })
    },
    handleMore(command, goodsId, row) {
      if (command === '1') {
        // 查看
        this.$router.push({ name: 'goodsView', params: { goodsId }})
      } else if (command === '2') {
        // 分享链接
        this.$refs.shareDialog.goodsId = goodsId
        this.$refs.shareDialog.dialogVisible = true
      } else if (command === '3') {
        this.goodsList = row
        // // 商品租赁频道
        this.$refs.leaseDialog.goodsId = goodsId
        this.$refs.leaseDialog.dialogVisible = true
      } else if (command === '4') {
        // console.log(goodsId)
        updateGoods({ goods_id: goodsId }).then(res => {
          // console.log(res)
        })
      }
    },
    handleCommand(command, goodsId) {
      if (command === '1') {
        this.$router.push({ name: 'goodsBaseEdit', params: { editId: goodsId }})
      } else {
        this.$router.push({ name: 'goodsMealInfo', params: { goodsId }})
      }
    },
    // 查询
    search() {
      const pickerVal = this.$refs.myDatePicker.getVal()
      if (pickerVal !== 'error') {
        this.searchForm.create_time = pickerVal
        this.$refs.customPage.page = 1
        this.getList()
      }
    },
    // 重置
    reset() {
      if (this.$refs.myDatePicker) {
        this.$refs.myDatePicker.reset()
      }
      this.searchForm = {
        goods_id: '',
        goods_name: '',
        shop_name: '',
        cate_id: '',
        rent_type: '',
        create_time: ''
      }
      this.getList()
    },
    handleClick(tab) {
      this.activeName = tab.name
      this.$refs.customPage.page = 1
      this.getList()
    },
    // 获取数据
    getList(flag) {
      // flag为true, 则不刷新表格
      const { page, pageSize } = this.$refs.customPage
      if (!flag) {
        this.tableData = []
      }
			const params1={
				page,
				pageSize,
				id:this.searchForm.goods_id,
				title:this.searchForm.goods_name,
				goodsCreateTime:''
			}
      getGoodsList(params1).then(res => {
        this.tableData = res.data || []
        this.total = res.total
        // for (const key in this.count) {
        //   this.count[key] = res.count[key]
        // }
      })
			const params={
				name:'',
				categoryCreateTime:'',
				page,
				pageSize
			}
			getCategoryList(params).then(res => {
			  this.categoryList = res.data || []
			})
    },
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      vm.getList(true)
    })
  }
}
</script>
<style lang="scss" scoped>
.common_form_search {
  padding: 15px 30px 0;
}

.child_page {
  padding-bottom: 15px;
}

.goods_info {
  .info {
    // margin-left: 10px;

    p {
      color: #666666;
    }

    p:first-child {
      color: #999999;
    }
  }
}

.activity {
  line-height: 24px;

  span {
    width: 20px;
    height: 20px;
    margin-left: 10px;
  }
}
</style>
